package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;

public class CuteToolbar extends Toolbar implements View.OnClickListener {

    public interface OnClickCuteToolbarListener {

        void onClickLeft();

        void onClickRight();
    }

    private OnClickCuteToolbarListener mOnClickCuteToolbarListener;

    private RelativeLayout mLayout;

    private CuteTextView mTitle;

    private CuteTextView mTextLeft;

    private CuteTextView mTextRight;

    private CuteImageView mImageLeft;

    private CuteImageView mImageRight;

    public CuteToolbar(Context context) {
        super(context);
        init(context, null);
    }

    public CuteToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CuteToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickCuteToolbarListener == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.cute_tv_left:
                mOnClickCuteToolbarListener.onClickLeft();
                break;
            case R.id.cute_tv_right:
                mOnClickCuteToolbarListener.onClickRight();
                break;
            case R.id.cute_iv_left:
                mOnClickCuteToolbarListener.onClickLeft();
                break;
            case R.id.cute_iv_right:
                mOnClickCuteToolbarListener.onClickRight();
                break;
        }
    }

    public void setOnClickCuteToolbarListener(OnClickCuteToolbarListener onClickCuteToolbarListener) {
        mOnClickCuteToolbarListener = onClickCuteToolbarListener;
    }

    public String getCuteTitle() {
        return mTitle.getText().toString();
    }

    public void setCuteTitle(String title) {
        mTitle.setText(title);
    }

    public void setCuteTitle(int resId) {
        mTitle.setText(resId);
    }

    public void setTextLeft(String text) {
        mTextLeft.setText(text);
    }

    public void setTextRight(String text) {
        mTextRight.setText(text);
    }

    public void setImageLeft(Drawable drawable) {
        mImageLeft.setCuteImageDrawable(drawable);
    }

    public void setImageRight(Drawable drawable) {
        mImageRight.setCuteImageDrawable(drawable);
    }

    public RelativeLayout getLayout(){
        return mLayout;
    }

    private void init(Context context, AttributeSet attrs) {
        setContentInsetsAbsolute(0, 0);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CuteToolbar);
        String cuteTitle = typedArray.getString(R.styleable.CuteToolbar_cute_title);
        String cuteLeftText = typedArray.getString(R.styleable.CuteToolbar_cute_left_text);
        String cuteRightText = typedArray.getString(R.styleable.CuteToolbar_cute_right_text);
        Drawable cuteLeftSrc = typedArray.getDrawable(R.styleable.CuteToolbar_cute_left_src);
        Drawable cuteRightSrc = typedArray.getDrawable(R.styleable.CuteToolbar_cute_right_src);
        typedArray.recycle();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_cute_toolbar, this);
        mLayout = (RelativeLayout) findViewById(R.id.cute_layout);
        mTitle = (CuteTextView) findViewById(R.id.cute_toolbar_title);
        mTextLeft = (CuteTextView) findViewById(R.id.cute_tv_left);
        mTextRight = (CuteTextView) findViewById(R.id.cute_tv_right);
        mImageLeft = (CuteImageView) findViewById(R.id.cute_iv_left);
        mImageRight = (CuteImageView) findViewById(R.id.cute_iv_right);

        setVisibility(mTitle, cuteTitle);
        setVisibility(mTextLeft, cuteLeftText);
        setVisibility(mTextRight, cuteRightText);
        setVisibility(mImageLeft, cuteLeftSrc);
        setVisibility(mImageRight, cuteRightSrc);

        mTextLeft.setOnClickListener(this);
        mTextRight.setOnClickListener(this);
        mImageLeft.setOnClickListener(this);
        mImageRight.setOnClickListener(this);
    }

    // 第一次点击
    private long firstClickTime = 0;

    // 双击事件
    public static interface OnDoubleClickListener {
        public void onDoubleClick();
    }

    public void setOnDoubleClickListener(OnDoubleClickListener doubleClickListener) {
        this.doubleClickListener = doubleClickListener;
    }

    // 双击监听
    private OnDoubleClickListener doubleClickListener;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            if(firstClickTime == 0) {
                firstClickTime = System.currentTimeMillis();
                return super.onTouchEvent(ev);
            }
            if(System.currentTimeMillis() - firstClickTime < 500) {
                // 处理双击事件
                if(doubleClickListener != null) {
                    doubleClickListener.onDoubleClick();
                }
            }
            firstClickTime = 0;
        }
        return super.onTouchEvent(ev);
    }

    private void setVisibility(CuteTextView view, String text) {
        if (!TextUtils.isEmpty(text)) {
            view.setText(text);
            view.setVisibility(View.VISIBLE);
        }
    }

    private void setVisibility(CuteImageView view, Drawable drawable) {
        if (drawable != null) {
            view.setCuteImageDrawable(drawable);
            view.setVisibility(View.VISIBLE);
        }
    }


}
